package com.iis.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import com.iis.entity.Agent;
import com.iis.entity.Client;

public class ShowTransactions {
	
	public static void ShowTransactionsTable(JPanel parent){
		
		JPanel titlePanel = new JPanel();
		titlePanel.setLayout(new BorderLayout());
		titlePanel.setBounds(20, 20, 550, 60);
		titlePanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1, true));
		
		JLabel lblTitle = new JLabel("Transaction Records");
		lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
		lblTitle.setFont(lblTitle.getFont().deriveFont(28.0f));
		titlePanel.add(lblTitle, BorderLayout.CENTER);
		titlePanel.revalidate();
		
		String col[] = {"Agent/Sales ID ","Customer Name","Insurance Type"};
		DefaultTableModel tableModel = new DefaultTableModel(col, 0);
		Object[] objs = {"AGENT-001", "Yustinus Widya", "Comprehensive"};
		for(int i=0;i<10;i++)
			tableModel.addRow(objs);
		
		JTable table = new JTable(tableModel);
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(20, 90, 550, 300);

		table.addMouseListener(new TableMouseListener(table));
		
		// constructs the popup menu
        /*JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem mniEditRow = new JMenuItem("Edit");
        JMenuItem mniDeleteRow = new JMenuItem("Delete");
        
        // mniEditRow
 		mniEditRow.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				// TODO Auto-generated method stub
 				int selectedRow = table.getSelectedRow();
 				Client client = new Client();
 				client.setRegistrationNo(table.getValueAt(selectedRow, 0).toString());
 				client.setName(table.getValueAt(selectedRow, 1).toString());
 				client.setCategory(table.getValueAt(selectedRow, 2).toString());
 				//tableModel.removeRow(selectedRow);
 				EditClientForm editClientForm = new EditClientForm(table, client);
 				editClientForm.setVisible(true);
 			}
 		});
 		
 		// mniDeleteRow
 		mniDeleteRow.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				// TODO Auto-generated method stub
 				int selectedRow = table.getSelectedRow();
 		        
 				int dialogButton = JOptionPane.YES_NO_OPTION;
                int dialogResult = JOptionPane.showConfirmDialog (null, "Are sure want to delete this record?","Warning",dialogButton);
                if(dialogResult == JOptionPane.YES_OPTION){ 
                	tableModel.removeRow(selectedRow);
                }
 			}
 		});
        
 		popupMenu.add(mniEditRow);
        popupMenu.add(mniDeleteRow);
		
        // sets the popup menu for the table
        table.setComponentPopupMenu(popupMenu);
        
        JButton btnAdd = new JButton("Add New");
        btnAdd.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				EditClientForm editClientForm = new EditClientForm(table);
 				editClientForm.setVisible(true);
			}
		});
        btnAdd.setBounds(445, 400, 120, 30);
        
        parent.add(btnAdd);*/
		parent.add(titlePanel, BorderLayout.CENTER);
		parent.add(scrollPane, BorderLayout.CENTER);
	}
}
